package agents.interaction;

import game.constants.ReferenceAgent;
import game.constants.ReferenceObject;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.HumanInteraction;

import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionGatherMineral;
import actions.ActionGiveMinerals;
import actions.ActionReciveMinerals;
import actions.ActionXYtoMove;
import extra.ObjectId;
import extra.Position;

public class InteractionGatherMineral extends HumanInteraction{
	
	ObjectId mineralId;
	

	ActionGiveMinerals actionGiveMineral;
	ActionXYtoMove actionXYtoMove;
	
	SpetialFeature timeToGather;
	SpetialFeature mineralsQuantityToGather;
	SpetialFeature mineralsQuantity;
	SpetialFeature attackRange;
	SpetialFeature xToMove;
	SpetialFeature yToMove;

	WorldObject commandCenterToCarryMinerals=null;
	
	double contTime=0;
	
	@Override
	public void initializeParametersList() {
		
		mineralId = new ObjectId();
		addParameter(mineralId);
		
	}

	@Override
	public void initializeActionList() {
		
		actionXYtoMove= new ActionXYtoMove();
		addAction(actionXYtoMove);

		actionGiveMineral= new ActionGiveMinerals();
		addAction(actionGiveMineral);
		
	}

	@Override
	public void initializeSpecialFeatures() {
		
		timeToGather = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TIME_TO_GATHER_MINERAL);
		addSpetialFeature(timeToGather);
		
		mineralsQuantityToGather= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_MINERAL_QUANTITY_TO_GATHER);
		addSpetialFeature(mineralsQuantityToGather);
		
		mineralsQuantity= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_MINERAL_QUANTITY);
		addSpetialFeature(mineralsQuantity);
		
		attackRange = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_ATTACK_RANGE);
		addSpetialFeature(attackRange);
		
		xToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_TO_MOVE);
		addSpetialFeature(xToMove);
		
		yToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_TO_MOVE);
		addSpetialFeature(yToMove);
	}

	@Override
	public void initializeName() {
		name= "Gather Minerals";
		
	}

	@Override
	public void initializeId() {
		this.id= ReferenceAgent.AGENT_HUMAN_INTERACTION_GATHER_MINERALS;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		
		object.desactivateAllHumanInteractions();
		isActive=true;
		
		if(mineralsQuantity.getValue()<mineralsQuantityToGather.getValue()){
			gatherMineral(object,universe,time);
			return;
		}
		returnMinerals(object,universe,time);
		
		
	}

	private void returnMinerals(WorldObject object, Universe universe,double time) {
		
		//System.out.println("tengo los minerales para llevarlos al centro de comando");
		ArrayList<WorldObject> listCommandCenter = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_COMMAND_CENTER1);
		
		
		double distance=Integer.MAX_VALUE;
		
		for(WorldObject obj : listCommandCenter){
			if(obj.getOwner()==object.getOwner()){
				double dAux = Position.DistanceBetween(obj.getPosition(), object.getPosition());
				if(dAux<distance){
					distance=dAux;
					commandCenterToCarryMinerals=obj;
				}
			}
		}
		
		if(commandCenterToCarryMinerals==null){
			return;
		}
		double x =commandCenterToCarryMinerals.getImageObject().getImageToShow().getWidth()*(0.66)+commandCenterToCarryMinerals.getPosition().getX();
		double y =commandCenterToCarryMinerals.getPosition().getY();
		distance = (Position.DistanceBetween(object.getPosition(), new Position(x,y)));
		
		if(distance>attackRange.getValue()){
			
			if(xToMove.getValue()==x && yToMove.getValue()==y){

				return;
			}
		
			actionXYtoMove.setSource(object);
			actionXYtoMove.setTarget(object);
			actionXYtoMove.setPosition(new Position(x,y));
			universe.setAction(actionXYtoMove);
			return;
		}
		
		actionGiveMineral.setSource(object);
		actionGiveMineral.setTarget(object);
		actionGiveMineral.setQuantity(mineralsQuantity.getValue());
		universe.setAction(actionGiveMineral);
		
		ActionReciveMinerals actionReciveMinerals = new ActionReciveMinerals();
		actionReciveMinerals.setQuantity(mineralsQuantity.getValue());
		actionReciveMinerals.setSource(object);
		actionReciveMinerals.setTarget(commandCenterToCarryMinerals);
		universe.setAction(actionReciveMinerals);
				
		
	}

	private void gatherMineral(WorldObject object,Universe universe, double time) {
		WorldObject mineral = universe.getObject(mineralId.getObjectId());
		if(mineral==null){
			return;
		}
		double distance = (Position.DistanceBetween(object.getPosition(), mineral.getPosition()));
		
		if(distance>attackRange.getValue()){
			if(xToMove.getValue()==mineral.getPosition().getX() && yToMove.getValue()==mineral.getPosition().getY()){
				return;
			}
			actionXYtoMove.setSource(object);
			actionXYtoMove.setTarget(object);
			actionXYtoMove.setPosition(mineral.getPosition());
			universe.setAction(actionXYtoMove);
			return;
		}
		
		contTime+=time;
		if(contTime<timeToGather.getValue()){
			return;
		}
		contTime=0;
		System.out.println("ya Recogi el mineral");
		ActionGatherMineral actionGatherMineral = new ActionGatherMineral();
		actionGatherMineral.setSource(object);
		actionGatherMineral.setTarget(mineral);
		universe.setAction(actionGatherMineral);
		
		
	}
	
	

}
